<template>
  <div class="flex flex-col h-full">
    <SearchContainer @reset="() => formRef.resetFields()" @search="getData()">
      <a-form ref="formRef" class="flex-1" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm" class="w-1/4">
          <a-input v-model:value="formState.zjm" placeholder="药品编号/药品名称/商品名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3">
        <BaseExport type="运维" name="中草药字典" code="00001142" :search-params="formState">导出</BaseExport>
        <!-- <a-button class="ml-2">导出</a-button> -->
      </div>
      <BaseTable
        :scroll="{ x: 2000 }"
        column-code="00001142"
        fill
        :data-source="tableData"
        bordered
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
          <template v-if="column.dataIndex === 'dj'">
            {{ record.dj }}
          </template>
          <template v-if="column.dataIndex === 'yxztStr'">
            <a-switch v-model:checked="record.yxzt" checked-value="0" un-checked-value="1" @change="onHandleDeactivate(record, $event)" />
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'

const { empName } = useUserStore()
const loading = ref(false)
const formRef = ref(null)
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const formState = ref({ cxxm: 2 })
const tableData = ref([])
const options = ref([])

const getData = async () => {
  loading.value = true
  const { code, data, total } = await post('/operation/opex/getDrugInfoList', {
    pageNum: page.value.current,
    pageSize: page.value.pageSize,
    ...formState.value
  })
  loading.value = false
  if (code === 0) {
    page.value.total = total
    tableData.value = data.map((item) => {
      item.yxzt = item.yxzt ? '1' : '0'
      return item
    })
  }
}
const getProjectList = async () => {
  const { code, data } = await post('/drugStorage/comprehenive/getProjectList')
  if (code !== 0) return
  options.value = data.map((item) => {
    return {
      label: item.mc,
      value: item.bh
    }
  })
}

const onHandleDeactivate = async (item, number) => {
  item.yxzt = number
  const { code, msg } = await post('/operation/opex/doModifyDrugStatus', {
    ...item,
    czr: empName,
    cztime: new Date()
  })
  if (code === 0) {
    message.success(msg)
    getData()
  }
}

const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  getData()
}

onMounted(() => {
  getData()
  getProjectList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
